Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.radarRecord = radarRecord, exports.isSelf = isSelf, exports.radarPush = radarPush, 
exports.radarResponse = radarResponse, exports.chatMsgSend = chatMsgSend, exports.createChatRoom = createChatRoom, 
exports.exitChatRoom = exitChatRoom, exports.gotoChatRoom = gotoChatRoom, exports.sharePushCheck = sharePushCheck, 
exports.radarService = radarService, exports.providerService = providerService, 
exports.MSG_TYPE = exports.radarTime = exports.VIEW_VALUES = exports.VIEW_TYPES = exports.RADAR_TYPES = void 0;

var _VIEW_VALUES, _api = _interopRequireDefault(require("./api")), _config = _interopRequireDefault(require("./config")), _user = _interopRequireDefault(require("../public/utils/user"));

function _interopRequireDefault(e) {
    return e && e.__esModule ? e : {
        default: e
    };
}

function _objectSpread(a) {
    for (var e = 1; e < arguments.length; e++) {
        var t = null != arguments[e] ? arguments[e] : {}, r = Object.keys(t);
        "function" == typeof Object.getOwnPropertySymbols && (r = r.concat(Object.getOwnPropertySymbols(t).filter(function(e) {
            return Object.getOwnPropertyDescriptor(t, e).enumerable;
        }))), r.forEach(function(e) {
            _defineProperty(a, e, t[e]);
        });
    }
    return a;
}

function _defineProperty(e, a, t) {
    return a in e ? Object.defineProperty(e, a, {
        value: t,
        enumerable: !0,
        configurable: !0,
        writable: !0
    }) : e[a] = t, e;
}

var MINI_TIME = 1;

function appData() {
    return getApp().globalData;
}

var RADAR_TYPES = {
    VOICE: "voice",
    CALL: "call",
    LIKE: "like",
    COLLECT: "collect",
    SHARE: "share",
    CARD: "viewcard",
    TIMECARD: "timeviewcard",
    ADDRESS: "viewaddress",
    PICTURE: "viewpicture",
    TIMEPICTURE: "timeimage",
    SYNCCONTACT: "synccontact",
    COPYEMAIL: "copyemail",
    COPYWECHAT: "copywechat",
    COPYMOBILE: "copymobile",
    CARDTAGS: "cardtags",
    TRANSFERCARD: "transfercard",
    VIEWTRANSFERCARD: "viewtransfercard",
    WATCHVIDEO: "watchvideo",
    EXCHANGECARD: "exchangecard",
    ACTIVITYSCAN: "activityscan",
    EXCHANGECARDSUCCESS: "exchangecardsuccess",
    CREATECARDFROMYOU: "createcardfromyou",
    VIEWCORPAUTHINFO: "viewauthenticationinformation",
    VIEWCARDBYCOLLEAGUE: "viewcardbycolleague",
    CLICKHOMETOWN: "clickhometown",
    CLICKSCHOOL: "clickschool"
};

exports.RADAR_TYPES = RADAR_TYPES;

var VIEW_TYPES = {
    SCAN: "scan",
    SHARE: "share"
}, VIEW_VALUES = (_defineProperty(_VIEW_VALUES = {}, (exports.VIEW_TYPES = VIEW_TYPES).SCAN, 1), 
_defineProperty(_VIEW_VALUES, VIEW_TYPES.SHARE, 2), _VIEW_VALUES);

exports.VIEW_VALUES = VIEW_VALUES, wx.interruptPush = !1;

var radarTime = {};

function radarRecord(e) {
    radarTime[e] = Date.now();
}

function isSelf(e) {
    var a = (appData().tokens || {}).uid;
    return a = a || e.uid, +e.uid == +a;
}

exports.radarTime = radarTime;

var isUnClaim = function(e) {
    return 0 === e.isclaim;
}, isDismission = function(e) {
    return 3 === e.grade;
}, isDissolve = function(e) {
    return 2 === e.corpnamestatus;
}, checkPush = function(e, a) {
    return isSelf(e) && a || isUnClaim(e) || isDismission(e) || isDissolve(e) || !e.isFeeVer;
};

function radarPush(e, a) {
    var t = 2 < arguments.length && void 0 !== arguments[2] ? arguments[2] : {};
    if (e && e.cardid && !checkPush(e, !0)) {
        var r = _objectSpread({
            node: a,
            cardid: e.cardid,
            app: _config.default.WX_APP_NAME
        }, t);
        if (radarTime[a]) {
            var i = Math.floor((Date.now() - radarTime[a]) / 1e3);
            r.timestamp = i || MINI_TIME, delete radarTime[a];
        }
        if (appData().isDevTools) return console.log("devtool:radar-push", r);
        _api.default.minicard.card.push({
            data: r
        });
    }
}

var MSG_TYPE = {
    textMsg: 1,
    sysMsg: 5,
    cardMsg: 7,
    exchangeMsg: 9
};

function radarResponse(e, r) {
    var i = !(2 < arguments.length && void 0 !== arguments[2]) || arguments[2];
    if (checkPush(e, i) || i && !e.isautoreply) return Promise.reject();
    if (appData().isDevTools) return console.log("devtool:radar-response");
    var a = appData().tokens.uid, t = e.uid, o = i ? a : t, n = i ? t : a;
    return createChatRoom({
        touid: o,
        uid: n
    }, i).then(function(e) {
        var a = e.data.chatid, t = {
            chatid: a,
            touid: o,
            app: _config.default.WX_APP_NAME,
            appid: _config.default.WX_APP_ID,
            content: r
        };
        i && (t.fromuid = n, t.issetnotread = 1), chatMsgSend(t).then(function() {
            return exitChatRoom({
                chatid: a
            });
        });
    });
}

function chatMsgSend(e) {
    return _api.default.chat.message.send({
        data: e
    });
}

function createChatRoom(e, a) {
    var t = _user.default.getTokensSync();
    return a || +t.uid != +e.touid ? _api.default.chat.chat.create({
        data: e
    }).then(function(e) {
        return e && 0 === e.code ? e : Promise.reject();
    }) : (wx.showModal({
        content: "自己不能跟自己对话哦",
        showCancel: !1
    }), Promise.reject());
}

function exitChatRoom(e) {
    return _api.default.chat.chat.exitchat({
        data: e
    });
}

function gotoChatRoom(e, t) {
    var r = e.touid, a = e.name, i = void 0 === a ? "" : a, o = e.cardid, n = void 0 === o ? "" : o;
    return wx.showLoading({
        title: "发起聊天中...",
        mask: !0
    }), createChatRoom({
        touid: r
    }).then(function(e) {
        var a = e.data.chatid;
        return t ? chatMsgSend({
            chatid: a,
            content: t,
            touid: r,
            app: _config.default.WX_APP_NAME,
            appid: _config.default.WX_APP_ID
        }).then(function() {
            return a;
        }).catch(function() {
            return a;
        }) : Promise.resolve(a);
    }).then(function(e) {
        wx.navigateTo({
            url: "/publicPackage/salesradar/minichat/minichat?name=".concat(encodeURIComponent(i), "&cardid=").concat(n, "&chatid=").concat(e, "&touid=").concat(r, "&app=").concat(_config.default.WX_APP_NAME)
        });
    }).catch(wx.hideLoading);
}

exports.MSG_TYPE = MSG_TYPE;

var SHARECARDPUSH = "share_card_radar_record";

function sharePushCheck(a) {
    var t = 1 < arguments.length && void 0 !== arguments[1] ? arguments[1] : {}, r = 2 < arguments.length ? arguments[2] : void 0, i = wx.getStorageSync(SHARECARDPUSH) || [], o = t.relatedid, e = t.shareuid;
    +a.uid != +e && !i.map(function(e) {
        return e.relatedid;
    }).includes(o) && o && _api.default.minicard.card.pushcheck({
        data: {
            relatedid: o,
            type: r ? 2 : 1
        }
    }).then(function(e) {
        0 == +e.data.status && radarPush(a, RADAR_TYPES[r ? "TRANSFERCARD" : "SHARE"], t), 
        wx.setStorage({
            key: SHARECARDPUSH,
            data: i.concat({
                relatedid: o
            })
        });
    });
}

var serviceInfo = {
    servername: "小名片客服",
    serveruid: 22430777
}, serverSelfTip = function() {
    wx.showModal({
        title: "温馨提示",
        content: "你就是客服",
        showCancel: !1
    });
};

function radarService() {
    providerService("", !1);
}

function providerService(e) {
    var a = !(1 < arguments.length && void 0 !== arguments[1]) || arguments[1], t = appData().card || {}, r = t.agent;
    t.guid || (t = appData().tokens, r = serviceInfo);
    var i = t, o = i.cardid, n = void 0 === o ? "" : o, c = i.uid, d = r, s = d.serveruid, u = d.servername;
    return +s == +c ? serverSelfTip() : +s === serviceInfo.serveruid && a ? gotoMiniService(e) : (wx.showLoading({
        mask: !0
    }), createChatRoom({
        touid: s
    }).then(function(e) {
        var a = e.data;
        return _api.default.chat.message.send({
            data: {
                chatid: a.chatid,
                app: _config.default.WX_APP_NAME,
                appid: _config.default.WX_APP_ID,
                content: {
                    content: "你好，我是".concat(u, "，欢迎使用小名片，在使用过程中有任何疑问都可以直接在这里跟我实时沟通噢！"),
                    type: MSG_TYPE.textMsg
                },
                touid: c,
                fromuid: s
            }
        }).then(function() {
            return a;
        });
    }).then(function(e) {
        var a = e.chatid, t = e.count;
        wx.navigateTo({
            url: "/publicPackage/salesradar/minichat/minichat?name=".concat(encodeURIComponent(u), "&chatid=").concat(a, "&cardid=").concat(n, "&num=").concat(t, "&touid=").concat(s)
        });
    }).then(wx.hideLoading, wx.hideLoading));
}

function gotoMiniService(a) {
    return new Promise(function(e) {
        wx.navigateToMiniProgram({
            appId: "wx0ac42dd945fd158d",
            path: "pages/chat/view/view?source=xmp&mode=create&guid=".concat(a),
            success: e
        });
    });
}